package com.youlu.campus.service.dingtalk;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest;
import com.dingtalk.api.response.OapiRobotSendResponse;
import com.taobao.api.ApiException;
import com.youlu.campus.entity.VO.req.DingTalkRobotRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class DingTalkService {

    /**
     * 支付投诉 -后期优化
     * @param dingTalkRobotRequest
     */
    public  void sendPayComplaintMsg(DingTalkRobotRequest dingTalkRobotRequest){
        String content = dingTalkRobotRequest.getContent();
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/robot/send?access_token=684169b0909cd7ac42699f96e8cc6193b65a7fbbe90af145c84d2a2fafdf8a31");
        OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown();
        markdown.setTitle("支付投诉");
        markdown.setText("# 当前累积待处理："+content+" 单"+"\n");
        OapiRobotSendRequest.At requestAt = new OapiRobotSendRequest.At();
        requestAt.setIsAtAll(Boolean.TRUE);
        OapiRobotSendRequest robotSendRequest = new OapiRobotSendRequest();
        robotSendRequest.setAt(requestAt);
        robotSendRequest.setMsgtype("markdown");
        robotSendRequest.setMarkdown(markdown);
        try {
            OapiRobotSendResponse response = client.execute(robotSendRequest);
            log.info("code ->{}| msg->{} ",response.getErrcode(),response.getErrmsg());
        } catch (ApiException e) {
            e.printStackTrace();
            log.error("发送失败",e);
        }catch (Exception e) {
            e.printStackTrace();
            log.error("发送失败",e);
        }

    }

    /**
     * 业务修改订单通知-后期优化
     * @param dingTalkRobotRequest
     */
    public  void sendBusniessDingTalkMarkdownMsg(DingTalkRobotRequest dingTalkRobotRequest){
        String title = dingTalkRobotRequest.getTitle();
        String content = dingTalkRobotRequest.getContent();
        String model = dingTalkRobotRequest.getModel();
        String optUser = dingTalkRobotRequest.getOptUser();
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/robot/send?access_token=eeded5905dd37831533ce0c2bd39851894a1562fd3392c9c97dbb8171a832ae8");
        OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown();
        markdown.setTitle("业务修改");
        markdown.setText("#### 修改来源平台："+title+"\n" +
                "#### 修改模块: "+model+"\n\n" +
                "#### 修改内容: 订单号【"+content+"】\n"  +
                "#### 操作人："+optUser+"\n");
        OapiRobotSendRequest.At requestAt = new OapiRobotSendRequest.At();
        requestAt.setIsAtAll(Boolean.TRUE);
        OapiRobotSendRequest robotSendRequest = new OapiRobotSendRequest();
        robotSendRequest.setAt(requestAt);
        robotSendRequest.setMsgtype("markdown");
        robotSendRequest.setMarkdown(markdown);
        //robotSendRequest.set
        try {
            OapiRobotSendResponse response = client.execute(robotSendRequest);
            log.info("code ->{}| msg->{} ",response.getErrcode(),response.getErrmsg());
        } catch (ApiException e) {
            e.printStackTrace();
            log.error("发送失败",e);
        }catch (Exception e) {
            e.printStackTrace();
            log.error("发送失败",e);
        }

    }

    public static void main(String[] args) {
        DingTalkRobotRequest dingTalkRobotRequest = new DingTalkRobotRequest();

        dingTalkRobotRequest.setContent("3");
       // dingTalkRobotRequest.setOptUser("刘云");
        //sendBusniessDingTalkMarkdownMsg(dingTalkRobotRequest);
       // sendPayComplaintMsg(dingTalkRobotRequest);

    }
}
